package com.uyayo.core.controller;

import com.uyayo.framework.web.util.WebUtil;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * <p>
 * Title: BaseController
 * </p>
 * <p>
 * Description:基础的controller
 * </p>
 * <p>
 * Company: www.lelekeji.com
 * </p>
 * 
 * @author 乐乐科技.胡逢君
 * @date 2015-4-13下午4:03:35
 * @version 1.0
 */
@Controller
@RequestMapping()
public class BaseController{

	/**
	 * 404页面
	 */
	@RequestMapping(value="/404",method=RequestMethod.GET)
	public ModelAndView error404(ModelAndView modelAndView){
		modelAndView.setViewName("error/404");
		return modelAndView;
	}

	/**
	 * 500页面
	 */
	@RequestMapping(value="/500",method=RequestMethod.GET)
	public ModelAndView error500(ModelAndView modelAndView){
		modelAndView.setViewName("error/500");
		return modelAndView;
	}

	/**
	 * 403页面
	 */
	@RequestMapping(value="/403",method=RequestMethod.GET)
	public String error403(HttpServletResponse response,HttpServletRequest request){
		if (WebUtil.isAjax(request)) {// ajax请求
			Map<String,String> resultMap = new HashMap<>();
			response.setStatus(HttpStatus.FORBIDDEN.value());
			WebUtil.out(response, resultMap);
			return null;
		}
		return "error/403";
	}

	/**
	 * 401页面
	 */
	@RequestMapping(value="/401",method=RequestMethod.GET)
	public ModelAndView error401(ModelAndView modelAndView){
		modelAndView.setViewName("error/401");
		return modelAndView;
	}

	/**
	 * 错误页面 
	 */
	@RequestMapping(value="/errors/{type}",method=RequestMethod.GET)
	public ModelAndView error(ModelAndView modelAndView,@PathVariable("type") String type){
		modelAndView.setViewName(type);
		return modelAndView;
	}
	
	/**
	 * 成功页面
	 */
	@RequestMapping(value="/success/{type}",method=RequestMethod.GET)
	public ModelAndView success(ModelAndView modelAndView,@PathVariable("type") String type){
		modelAndView.setViewName(type);
		return modelAndView;
	}
	
}


